package com.hp.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hp.mapper.UserMapper;
import com.hp.pojo.User;
import com.hp.service.UserService;
import com.hp.util.JsonData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;
    @Override
    public JsonData login(User user) {
        User login = userMapper.login(user);
        if (login==null){
            return JsonData.buildError("登录失败用户名或密码错误");
        }
        return JsonData.buildSuccess(login,"登录成功");
    }
    @Override
    public JsonData save(User user) {
        int i=0;
        if (user.getUserid()!=null){
            i=userMapper.edit(user);
        }else {
            i= userMapper.add(user);
        }
        if (i>0){
            return JsonData.buildSuccess("success");
        }
        return JsonData.buildError("error");
    }

    @Override
    public JsonData all(int page, int limit) {
        PageHelper.startPage(page, limit);
        List<User> all = userMapper.all();
        PageInfo<User> pageInfo=new PageInfo<>(all);
        return JsonData.buildSuccess(pageInfo,"success");
    }

    @Override
    public JsonData dle(int userid) {
        int i = userMapper.del(userid);
        if (i>0){
            return JsonData.buildSuccess("success");
        }
        return JsonData.buildError("error");
    }

    @Override
    public JsonData likeAll(User user,int page,int limit) {
        PageHelper.startPage(page,limit);
        List<User> users = userMapper.likeAll(user);
        PageInfo<User> pageInfo=new PageInfo<>(users);
        return JsonData.buildSuccess(pageInfo,"success");
    }

}
